package org.cb.scapegoat;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;

import org.cb.core.ColourBot;
import org.cb.core.Global;

/**
 * 
 * @author Fsig
 * 
 * This class will be used to find the inventory,
 * Identify all the slots, counts the free slots,
 * check if its full, check what item is in the slot
 * and select a slot/item?
 * 
 * They are the goals anyways.
 * 
 * This can become faster if instead of searching the 
 * entire slot we only search the middle 100 pixels.
 * Will work on implementing this later.
 *
 */
public class Inventory{
	public static int invStartX = 0;
	public static int invStartY = 0;
	private static int invWidth = 190;
	private static int invHeight = 250;
	
	private static Color invBeltColour = new Color(206,162,41);
	private static Color lY = new Color(255,190,58);
	private static Color dY = new Color(230,154,58);
	//private static Color swordRed = new Color(163,52,14);
	
	private static Color[] iCs = {new Color(41,36,33),
									new Color(49,40,33),
									new Color(58,49,41),
									new Color(58,53,41),
									new Color(66,61,49),
									new Color(74,65,58),
									new Color(66,57,41),
									new Color(58,53,41),
									new Color(41,36,25),
									new Color(33,32,25),
									new Color(41,40,25),
									new Color(33,36,25),
									new Color(49,45,33),
									new Color(49,49,33),
									new Color(41,40,33),
									new Color(58,53,49)};
	
	private static Vector<Color> inventoryColours = new Vector<Color>();
	
	public static Rectangle inventory;
	public static Rectangle[] slots = new Rectangle[28];
	
	public static int slotWidth = 37;
	public static int slotHeight = 36;
	
	/**
	 * Set our inventory positions.
	 * 
	 * @throws InterruptedException
	 */
	public static void setInventory() {
		invStartX = 548;
		invStartY = 212;
		inventory = new Rectangle(invStartX, invStartY, invWidth, invHeight);
		
		slots[0] = new Rectangle(invStartX + 11, invStartY -3, slotWidth, slotHeight);
		slots[1] = new Rectangle(invStartX + 53, invStartY -3, slotWidth, slotHeight);
		slots[2] = new Rectangle(invStartX + 95, invStartY -3, slotWidth, slotHeight);
		slots[3] = new Rectangle(invStartX + 137, invStartY -3, slotWidth, slotHeight);
		slots[4] = new Rectangle(invStartX + 11, invStartY +33, slotWidth, slotHeight);
		slots[5] = new Rectangle(invStartX + 53, invStartY + 33, slotWidth, slotHeight);
		slots[6] = new Rectangle(invStartX + 95, invStartY + 33, slotWidth, slotHeight);
		slots[7] = new Rectangle(invStartX + 137, invStartY + 33, slotWidth, slotHeight);
		slots[8] = new Rectangle(invStartX + 11, invStartY + 69, slotWidth, slotHeight);
		slots[9] = new Rectangle(invStartX + 53, invStartY + 69, slotWidth, slotHeight);
		slots[10] = new Rectangle(invStartX + 95, invStartY + 69, slotWidth, slotHeight);	
		slots[11] = new Rectangle(invStartX + 137, invStartY + 69, slotWidth, slotHeight);
		slots[12] = new Rectangle(invStartX + 11, invStartY + 105, slotWidth, slotHeight);
		slots[13] = new Rectangle(invStartX + 53, invStartY + 105, slotWidth, slotHeight);
		slots[14] = new Rectangle(invStartX + 95, invStartY + 105, slotWidth, slotHeight);
		slots[15] = new Rectangle(invStartX + 137, invStartY + 105, slotWidth, slotHeight);
		slots[16] = new Rectangle(invStartX + 11, invStartY + 141, slotWidth, slotHeight);
		slots[17] = new Rectangle(invStartX + 53, invStartY + 141, slotWidth, slotHeight);
		slots[18] = new Rectangle(invStartX + 95, invStartY + 141, slotWidth, slotHeight);
		slots[19] = new Rectangle(invStartX + 137, invStartY + 141, slotWidth, slotHeight);
		slots[20] = new Rectangle(invStartX + 11, invStartY + 177, slotWidth, slotHeight);
		slots[21] = new Rectangle(invStartX + 53, invStartY + 177, slotWidth, slotHeight);
		slots[22] = new Rectangle(invStartX + 95, invStartY + 177, slotWidth, slotHeight);
		slots[23] = new Rectangle(invStartX + 137, invStartY + 177, slotWidth, slotHeight);	
		slots[24] = new Rectangle(invStartX + 11, invStartY + 213, slotWidth, slotHeight);
		slots[25] = new Rectangle(invStartX + 53, invStartY + 213, slotWidth, slotHeight);
		slots[26] = new Rectangle(invStartX + 95, invStartY + 213, slotWidth, slotHeight);
		slots[27] = new Rectangle(invStartX + 137, invStartY + 213, slotWidth, slotHeight);

		for(int i = 0; i < iCs.length; i++)
			inventoryColours.add(iCs[i]);
	}
	
	/**
	 * Find the Inventory Tab.
	 * 
	 * @return Point
	 * @throws InterruptedException
	 */
	public static Point findInventoryTab() throws InterruptedException{
		try{
			Point tabY = ColourBot.findColour(Global.getTabArea(), invBeltColour, 0);
			int x = (int) ((tabY.x - 5) + Global.getRandom().nextInt(11));
			int y = (int) ((tabY.x - 5) + Global.getRandom().nextInt(11));

			if(tabY != null)
				return new Point(x,y);
		
		}catch(Exception e){ColourBot.log("Failed to find the Inventory Tab, either you have not got the scapegoat " +
										"window visable or the colours have changed.");}
		return null;
	}
	
	/**
	 * This will compare the pixel colours of the inventory
	 * being normal as to when its selected. If the boarder 
	 * shows that it is selected then return true, else false.
	 */
	public static boolean isSelected() throws InterruptedException{
		if(ColourBot.checkColour(Global.getTabArea(), lY, new Point(132,1),0) && ColourBot.checkColour(Global.getTabArea(), dY, new Point(133,1),0))
			return true;

		return false;
	}

	/**
	 * This will use the find method to find the inventory
	 * tab and then click it.
	 */
	public static void click() throws InterruptedException{
		Point p = findInventoryTab();
		//if(p != null)
			//Mouse.leftClick(p);
	}
	
	/**
	 * 
	 * @return int
	 * @throws InterruptedException
	 */
	public static int countFreeSlots() throws InterruptedException{
		int count = 0;

		for(int i = 0; i < slots.length; i++){
			if(checkSlotIsEmpty(i))
				count++;
		}
		return count;
	}
	
	/**
	 * Check if the slot is free or not.
	 * 
	 * @param i
	 * @return boolean
	 * @throws InterruptedException
	 */
	public static boolean checkSlotIsEmpty(int i) throws InterruptedException{
		Color c[] = ColourBot.getColours(new Rectangle((int)slots[i].getCenterX() - 5, (int)slots[i].getCenterY() -5, 10,10));
		for(Color b : c){
			if(!inventoryColours.contains(b))
				return false;
		}
		return true;
	}
	
	/**
	 * Method to left click the selected slot.
	 * 
	 * @param slotID
	 * @throws InterruptedException
	 */
	public static void leftClick(int slotID) throws InterruptedException{
		int x = (int) ((slots[slotID].getCenterX() - 5) + Global.getRandom().nextInt(11));
		int y = (int) ((slots[slotID].getCenterY() - 5) + Global.getRandom().nextInt(11));
			
		//Mouse.leftClick(new Point(x,y));
	}
	
	/**
	 * Method to right click the selected slot.
	 * 
	 * @param slotID
	 * @throws InterruptedException
	 */
	public static void rightClick(int slotID) throws InterruptedException{
		int x = (int) ((slots[slotID].getCenterX() - 5) + Global.getRandom().nextInt(11));
		int y = (int) ((slots[slotID].getCenterY() - 5) + Global.getRandom().nextInt(11));
			
		//Mouse.rightClick(new Point(x,y));
	}

}
